<template>
  <div class="login-wrap">
    <div class="ms-login">
      <div class="ms-title">{{sysName}}</div>
      <el-form :model="registerForm" ref="registerForm" label-width="0px" class="ms-content">

        <el-form-item prop="userName">
          <el-input v-model.trim="registerForm.userName" placeholder="真实姓名">
            <template #prepend>
              <el-button icon="el-icon-s-custom"></el-button>
            </template>
          </el-input>
        </el-form-item>

        <el-form-item prop="loginUser">
          <el-input v-model.trim="registerForm.loginUser" placeholder="登录用户名">
            <template #prepend>
              <el-button icon="el-icon-user"></el-button>
            </template>
          </el-input>
        </el-form-item>
        <el-form-item prop="password">
          <el-input type="password" placeholder="密码" v-model="registerForm.password" @keyup.enter="register">
            <template #prepend>
              <el-button icon="el-icon-lock"></el-button>
            </template>
          </el-input>
        </el-form-item>

        <div class="login-btn">
          <el-button type="primary" @click="register">注册</el-button>
        </div>
        <p class="login-tips">Tips : 登录用户名有且唯一。</p>

      </el-form>
    </div>
  </div>
</template>

<script>

  import http from '@/utils/http';
  import {desEncrypt} from "../../utils/utils";
  import setting from '@/config/setting';

  export default {
    name: "Register",
    data() {
      return {
        registerForm: {
          // 登录用户名
          loginUser: '',
          // 密码
          password: '',
          // 真实姓名
          userName: '',
        },
        // 系统名称
        sysName: setting.sysName,
      }
    },
    methods: {
      /**
       * 提交注册
       */
      register(){
        let localStorage = window.localStorage;
        let loginPasswordKey = localStorage.loginPasswordKey

        // 加密key需要后端提供
        let encryptUser = desEncrypt(this.registerForm.loginUser, loginPasswordKey);
        let encryptPassword = desEncrypt(this.registerForm.password, loginPasswordKey);

        http.post("/open/register", {
          loginUser: encryptUser,
          password: encryptPassword,
          userName: this.registerForm.userName,
        }).then(response => {
          let res = response.data;
          if (res.status === 'error'){
            if (res.code !== 401){
              this.$message.error(res.msg);
            }
            return
          }

          this.$alert('恭喜!注册成功', '', {
            confirmButtonText: '确定',
            callback: action => {
              this.$router.push({path: "/"})
            }
          });

        }).catch(error => {
          this.$message.error(error.message);
        })
      },
    },
  }
</script>

<style scoped>


  .login-wrap {
    position: relative;
    width: 100%;
    height: 100%;
    /*background-image: url(../../assets/images/bg/avatar.png);*/
    /*background-image: url(../../assets/images/bg/background.jpg);*/
    background-size: 100%;
  }
  .ms-title {
    width: 100%;
    line-height: 50px;
    text-align: center;
    font-size: 20px;
    /*color: #fff;*/
    color: #304156;
    border-bottom: 1px solid #ddd;
  }
  .ms-login {
    position: absolute;
    left: 50%;
    top: 50%;
    width: 350px;
    margin: -190px 0 0 -175px;
    border-radius: 5px;
    background: rgba(255, 255, 255, 0.3);
    overflow: hidden;
  }
  .ms-content {
    padding: 30px 30px;
  }
  .login-btn {
    text-align: center;
  }
  .login-btn button {
    width: 100%;
    height: 36px;
    margin-bottom: 10px;
  }
  .login-tips {
    font-size: 12px;
    line-height: 30px;
    /*color: #fff;*/
    color: #304156;
  }

</style>
